<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      :close-on-click-modal="false"
      :title="title"
      @close="assignConClose"
      width="591px"
      class="assign_contract_dialog"
    >
      <el-form
        ref="form"
        :model="contractForm"
        :rules="contractRules"
        label-width="80px"
        label-position="top"
        class="get_form"
        :inline="true"
        
      >
        <div class="flex_box">
          <el-form-item label="关联学员" prop="customerId" style="width: 259px">
            <crm-relative-cell
              :value="contractForm.customerId"
              relative-type="customer"
              @value-change="otherChange"
            />
          </el-form-item>
          <el-form-item
            label="销售负责人"
            prop="ownerUserId"
            style="width: 259px"
          >
            <UserSelect
              v-model="contractForm.ownerUserId"
              @input="userChange"
              style="width: 100%"
            ></UserSelect>
          </el-form-item>
        </div>
        <!-- <el-form-item
          label="助教协作人"
          prop="assistantUserId"
          style="width: 259px"
        >
          <UserSelect
            v-model="contractForm.assistantUserId"
            @input="user2Change"
            style="width: 100%"
          ></UserSelect>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="assignConClose">取 消</el-button>
        <el-button type="primary" @click="assignConSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { chinaMobileRegex } from "@/utils";
import UserSelect from "@/views/admin/components/UserSelect";
import CrmRelative from "@/components/CreateCom/CrmRelative";
import { CrmRelativeCell } from "@/components/CreateCom";
import { allocationOrderPoolUser } from "@/api/crm/contractPool";
export default {
  /**
   * 账单-分配
   */
  name: "GetsContract",
  components: {
    UserSelect,
    CrmRelative,
    CrmRelativeCell,
  },
  data() {
    return {
      visible: false,
      contractForm: {
        // assistantUserId: "", //助教协作人
        ownerUserId: "", //销售负责人
        customerId: [], //绑定的学员Id
      },
      contractRules: {
        // assistantUserId: [
        //   { required: true, message: "助教协作人不能为空", trigger: "change" },
        // ],
        ownerUserId: [
          { required: true, message: "销售不能为空", trigger: "change" },
        ],
        customerId: [
          { required: true, message: "关联学员不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    assignConDialog: {
      handler(val) {
        if (val) {
          this.visible = true;
        } else {
          this.visible = false;
        }
      },
      immediate: true,
    },
  },
  props: ["assignConDialog", "title", "rowDatas"],
  methods: {
    otherChange(data, field) {
      if (data.value && data.value.length > 0) {
        this.contractForm.customerId = data.value;
      } else {
        this.contractForm.customerId = [];
      }
      this.$refs.form.validateField("customerId");
    },
    /**
     * 弹框-关闭
     */
    assignConClose() {
      this.contractForm = {
        // assistantUserId: "",
        ownerUserId: "",
        customerId: [],
      };
      this.$refs.form.resetFields();
      this.visible = false;
    },
    /**
     * 弹框-确定
     */
    assignConSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          allocationOrderPoolUser({
            // assistantUserId: this.contractForm.assistantUserId,
            ownerUserId: this.contractForm.ownerUserId,
            customerId: this.contractForm.customerId[0].customerId,
            ids: [this.rowDatas.contractId],
          }).then((res) => {
            if (res.code == 0) {
              this.$emit("submit");
              this.$message.success(res.msg);
              this.visible = false;
            } else {
              this.$message.error(res.msg, 2000);
            }
          });
        }
      });
    },
    /**
     *  学员-change
     */
    userChange(data) {
      this.contractForm["ownerUserId"] = data;
    },
    /**
     *  助教协作人-change
     */
    // user2Change(data) {
    //   this.contractForm["assistantUserId"] = data;
    // },
  },
};
</script>
<style scoped lang="scss">
.assign_contract_dialog {
  /deep/ .el-dialog__header {
    padding-top: 25px !important;
  }
  /deep/ .el-dialog__body {
    padding: 14px 23px !important;
  }
  .get_form {
    /deep/ .el-form-item__label {
      line-height: 6px !important;
    }
  }
  /deep/ .el-form--inline .el-form-item {
    margin-right: 0 !important;
  }
}
.flex_box {
  display: flex;
  justify-content: space-between;
}
</style>